﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lm.Common.ExtensionMethods
{
    public static class StringExt
    {
        /// <summary>
        /// 是否为一个枚举
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsEnum<T>(this string str)
        { return Enum.IsDefined(typeof(T), str); }

        /// <summary>
        /// 转换成一个枚举
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T ToEnum<T>(this string str)
        {
            return (T)Enum.Parse(typeof(T), str);
        }
        /// <summary>
        /// 是否为null或空白
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNullOrBlank(this string str)
        {
            if (str == null || str.Trim().Length == 0)
            { return true; }
            else
            { return false; }
        }

        public static bool IsInt32(this string str)
        {
            try
            {
                int i;
                return Int32.TryParse(str.Trim(), System.Globalization.NumberStyles.Any, null, out i);
            }
            catch { return false; }
        }
        public static int ToInt32(this string str)
        {
            return Int32.Parse(str.Trim(), System.Globalization.NumberStyles.Any);
        }

        public static bool IsDecimal(this string str)
        {
            try
            {
                decimal i;
                return decimal.TryParse(str.Trim(), System.Globalization.NumberStyles.Any, null, out i);
            }
            catch { return false; }
        }
        public static decimal ToDecimal(this string str)
        {
            return decimal.Parse(str.Trim(), System.Globalization.NumberStyles.Any, null);
        }

        public static bool IsInt64(this string str)
        {
            try
            {
                Int64 i;
                return Int64.TryParse(str.Trim(), System.Globalization.NumberStyles.Any, null, out i);
            }
            catch { return false; }
        }
        public static long ToInt64(this string str)
        {
            return Int64.Parse(str.Trim(), System.Globalization.NumberStyles.Any, null);
        }

        public static bool IsDouble(this string str)
        {
            try
            {
                double i;
                return double.TryParse(str.Trim(), System.Globalization.NumberStyles.Any, null, out i);
            }
            catch { return false; }
        }
        public static double ToDouble(this string str)
        {
            return double.Parse(str.Trim(), System.Globalization.NumberStyles.Any, null);
        }

        public static bool IsFloat(this string str)
        {
            try
            {
                float i;
                return float.TryParse(str.Trim(), System.Globalization.NumberStyles.Any, null, out i);
            }
            catch { return false; }
        }
        public static float ToFloat(this string str)
        {
            return float.Parse(str.Trim(), System.Globalization.NumberStyles.Any, null);
        }

        public static bool IsDatetime(this string str)
        {
            try
            {
                DateTime i;
                return DateTime.TryParse(str.Trim(), out i);
            }
            catch { return false; }
        }
        public static DateTime ToDatetime(this string str)
        {
            return Convert.ToDateTime(str);
        }

        public static bool IsBoolean(this string str)
        {
            try
            {
                bool i;
                return bool.TryParse(str.Trim(), out i);
            }
            catch { return false; }
        }
        public static bool ToBoolean(this string str)
        {
            return Convert.ToBoolean(str);
        }

        public static bool IsGuid(this string str)
        {
            try
            {
                Guid i;
                return Guid.TryParse(str.Trim(), out i);
            }
            catch { return false; }
        }
        public static Guid ToGuid(this string str)
        {
            return new Guid(str);
        }

        /// <summary>        
        /// Excel的日期，实际存储的是一个浮点数，1899-12-30加上这个浮点数，得出真正的时间
        /// </summary>
        /// <param name="str.Trim()"></param>
        /// <returns></returns>
        public static DateTime ToDateTimeForExcel(this string str)
        {
            double days = str.Trim().ToDouble();
            return "1899-12-30 0:00:00".ToDatetime().AddDays(days);
            //Excel counts an extra day for 1900-Feb-29. In reality, 1900 is not a leap year.
            //if (SharedResource.BaseDate == DateTime.Parse("1899-12-31") && days > 59)
            //{
            //    days--;
            //}
            //return SharedResource.BaseDate.AddDays(days);
        }
    }
}
